<template>
  <div class="container">
    <div class="navTop">
      <el-form :model="paginationList" label-width="100px">
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="用户名称:" prop="realName">
              <el-input
              clearable
                v-model="paginationList.realName"
                placeholder="请输入用户名称"
                size="large"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="手机号:" prop="phone">
              <el-input
              clearable
                placeholder="请输入用户名称"
                size="large"
                v-model="paginationList.phone"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="状态:" prop="status">
              <el-select
              clearable
                placeholder="请选择状态"
                size="large"
                v-model="paginationList.status"
              >
              <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option></el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="16">
            <el-form-item label="创建时间:" size="large" prop="createTime">
              <el-date-picker
              clearable
                type="datetimerange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                v-model="paginationList.createTime"
              />
            </el-form-item>
          </el-col>

          <el-col :span="2">
            <el-form-item class="search">
              <el-button style="background-color:#05CD95;color:#fff" round size="large" @click="getUserListAPI">搜索</el-button>
            </el-form-item>
          </el-col>

          <el-col :span="2">
            <el-form-item class="reset">
              <el-button plain round size="large" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="content">
      <div class="choice">
        <el-row :gutter="24">
          <el-col :span="12"><span class="choiceList">已选<i>{{selectedKeys.length}}</i>项</span></el-col>
          <el-col :span="11" style="display: flex; justify-content: end; margin-top: 33px">
            
            <el-button type="danger" round size="large" @click="showDialog = true">新增</el-button>
            <el-button round size="large" @click="delUser">删除</el-button>             
            <el-button round size="large">导入</el-button>        
            <el-button round size="large" @click="exportUser">导出</el-button>
         

          </el-col>
        </el-row>
      </div>
      <el-table style="width: 100%" :data="list" @select="delCheckoutList">
        <el-table-column align="center" prop="id" type="selection" label="用户编号"></el-table-column>
        <el-table-column align="center" prop="realName" label="用户名称"></el-table-column>
        <el-table-column align="center" prop="account" label="账号"></el-table-column>
        <el-table-column align="center" prop="deptId" label="部门"></el-table-column>
        <el-table-column align="center" prop="phone" label="手机号码"></el-table-column>
        <el-table-column align="center" prop="status" label="状态">
          <template v-slot="{ row }">
            <span v-if="row?.status === 1">启用</span>
            <span v-else>禁用</span>
          </template>
        </el-table-column>

        <el-table-column align="center" prop="createTime" label="创建时间"></el-table-column>
        
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <el-button type="text" @click="editList(row)">修改</el-button>
            <el-button type="text" @click="resetPassword">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>

      <e-row
        type="flex"
        justify="center"
        style="display: flex; justify-content: center; align-items: center"
      >
        <el-pagination
        style="margin-top:30px"
          v-model:current-page="page"
          v-model:page-size="size"
          :page-sizes="[100, 200, 300, 400]"
          :small="small"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="paginationList.total"
          @current-change="changePage"
        ></el-pagination>
      </e-row>
    </div>
    <AddUser
      v-model="showDialog"
      ref="editListChild"
      @closeDialog="closeDialog"
      @addUserAPI="getUserListAPI"
    ></AddUser>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getUserList, DowTemplate, delUserList } from '@/api/userControl'
import AddUser from './components/add-user.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import FileSaver from 'file-saver'

// 声明一个空数组来接收数据
const list = ref([])

// 分页数据
const paginationList = ref({
  page: 1,
  size: 10,
  total: 0
})

// 分页跳转的方法
const changePage = (newPage) => {
  paginationList.value.page = newPage
  getUserListAPI()
}


// 定义状态数据
const options = ref([
  { label: '启用', value: 'normal' },
  { label: '禁用', value: 'ban' }
])
// 挂载完执行
onMounted(() => {
  getUserListAPI()
})

// 获取主页用户数据
const getUserListAPI = async () => {
  const res = await getUserList(paginationList.value)
  list.value = res.data
  // console.log(rows)
  paginationList.value.total = res.total
}

// 重置模糊搜索方法
// const formData = ref(null)
const resetForm = () => {
  // formData.value.resetFields()
  paginationList.value.realName = ''
  paginationList.value.phone = ''
  paginationList.value.status = ''
  paginationList.value.createTime = ''
  getUserListAPI()
}

// 声明一个子组件名字的方法
const editListChild = ref(null)

const editList = (row) => {
  // console.log(row)

  showDialog.value = true
  editListChild.value.editConfirm(row)
}

const selectedKeys = ref({
  ids:[]
})
// 批量删除
const delCheckoutList = (selection) => {
  // console.log(selection)
  selectedKeys.value.ids=selection.map(item=> item.id)
  // selectedKeys.value.push(selection[0].id)
  // console.log(selectedKeys.value)
}

const delUser = () => {
  // console.log(selectedKeys.value)
  if (selectedKeys.value.length === 0) {
    ElMessage({
      type: 'warning',
      message: '请选择需要删除的行'
    })
  } else {
    ElMessageBox.confirm('确定要删除吗?', 'Warning', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
      .then(async () => {
        await delUserList(selectedKeys.value)
        // console.log(selectedKeys.value)
        
        ElMessage({
          type: 'success',
          message: '删除成功'
        })
        selectedKeys.value = []
        getUserListAPI()
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '删除失败',
        })
      })
  }
}

// 导出
const exportUser = async () => {
  const resf = await DowTemplate()
  FileSaver.saveAs(resf, '用户信息表.xlsx') // 下载文件
}

// 重置密码
const resetPassword = () => {
  ElMessageBox.confirm('确定重置密码吗?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    center: true
  })
    .then(() => {
      ElMessage({
        type: 'success',
        message: '重置成功,默认密码为123456'
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '重置失败'
      })
    })
}

const showDialog = ref(false)

const closeDialog = () => {
  showDialog.value = false
}
</script>

<style lang="less" scoped>
.navTop {
  background-color: #fff;
}

.content {
  // padding: 0px 20px;
  .choice {
    height: 100px;
    width: 100%;
  }
  .choiceList {
    display: inline-block;
    margin-left: 20px;
    margin-top: 50px;
    i {
      display: inline-block;
      font-style: normal;
      color: #05cd95;
    }
  }

  .feature {
    margin-top: 10px;
  }
}
.el-form-item {
  display: block;
  width: 80%;
  margin-left: 30px;
}
.el-input {
  margin-left: 25px;
}
.el-select {
  margin-left: 25px;
  width: 100%;
}

.el-date-picker {
  width: 50%;
}

::v-deep .el-form-item--large .el-form-item__content {
  width: 45%;
  margin-left: 25px;
}

.search {
  margin-top: 40px;
  margin-left: 120px;
}
.reset {
  margin-top: 40px;
  margin-left: 100px;
}
</style>


